#/bin/bash
serviceName='sb2-only'
version='1.0.0'
port='9999'
dockerRepo='192.168.200.179'
dockerUsername='admin'
dockerPassword='123456'
dockerNamespace='project'
dockerPort='9999'
nacosDiscovery='192.168.200.179'
nacosNamespace='11'

containerId=`docker ps -a | grep $serviceName | awk '{print $1}'`
if [ [$containerId] ]; then
  echo '删除正在运行的容器服务'
  docker stop -t 60 "$containerId"
  docker rm "$containerId"
fi

echo '删除本地镜像'
imageId=`docker images | grep $serviceName | awk '{print $1":"$2}'`
if [ [$imageId] ]; then
    docker rmi "$imageId"
fi

echo '拉取最新镜像'
docker login -u $dockerUsername -p $dockerPassword $dockerRepo
imageName=$dockerRepo/$dockerNamespace/$serviceName:$version
docker pull $imageName

echo '开始运行服务'
#docker_id= $(docker run -d --name $serviceName -p $port:dockerPort -v /app/jar/bi/logs:/logs --entrypoint="" $imageName java -Xms256m -Xmx512m -Djava.security.egd=file:/dev/./urandom -Duser.timezone=GMT+08 -server -cp /app/resources:/app/classes:/app/libs/* com.yurun.IauServiceApplication --spring.cloud.nacos.discovery.ip=$nacosDiscovery --spring.cloud.nacos.config.namespace=$nacosNamespace)
docker_id= $(docker run -d --name $serviceName -p $port:dockerPort $imageName java -Xms256m -Xmx512m com.yurun.IauServiceApplication)

res=$(docker ps | grep $serviceName)
if [ [$res] ]; then
    echo "服务已启动,检查是否成功上线"
    echo "$res"
    exit 0;
else
    echo "服务启动失败"
    exit 1;
fi